package com.hzm.test.service;

import com.hzm.test.config.CacheManagerNames;
import com.hzm.test.model.Config;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@Component
public class ConfigService {

    @Cacheable(value = "config", key = "#key", unless = "#result==null || #result.size() == 0",
            cacheManager = CacheManagerNames.CONCURRENT_MAP_CACHE_MANAGER)
    public List<Config> listRankByCache(String key) {
        System.out.println("listRankByCache: ");
        return listRank(key);
    }

    @CachePut(value = "config", key = "#key", unless = "#result==null || #result.size() == 0",
            cacheManager = CacheManagerNames.CONCURRENT_MAP_CACHE_MANAGER)
    public List<Config> listRankCacheUpdate(String key) {
        System.out.println("listRankCacheUpdate: ");
        return listRank(key);
    }

    private List<Config> listRank(String clubCode) {
        List<Config> list = new LinkedList<>();

        int length = new Random().nextInt(3);

        System.out.println("-----length: " + length);

        for (int i = 1; i <= length; i++) {
            list.add(new Config(new Random().nextInt(i*100) + "_uid", clubCode));
        }

        return list;
    }
}
